package com.loong.shop.controller.backend;

import com.loong.common.mvc.BaseBackendController;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.shop.common.backend.param.GoodsCategoryParam;
import com.loong.shop.handler.GoodsCategoryHandler;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 处理后台商品类别请求的Controller
 * @Author: 刘群
 * @Date: 2020/10/27 8:55
 */
@RestController
@RequestMapping("manage/shop/goodsCategory")
public class GoodsCategoryController extends BaseBackendController {

    @Resource
    private GoodsCategoryHandler categoryHandler;

    /**
     * 新增商品类别
     * @param param 商品类别封装的参数
     * @return 响应的结果
     */
    @PostMapping("save")
    public Result saveGoodsCategory(@Validated({CommonInsert.class}) GoodsCategoryParam param){
        param.setId(null);
        return handlerExecutor.execute(() -> categoryHandler.saveGoodsCategory(param));
    }

    /**
     * 更新商品类别
     * @param param 商品类别封装的参数
     * @return 响应的结果
     */
    @PostMapping("update")
    public Result updateGoodsCategory(@Validated({CommonUpdate.class}) GoodsCategoryParam param){
        return handlerExecutor.execute(() -> categoryHandler.updateGoodsCategory(param));
    }

    /**
     * 删除商品类别
     * @param idList 商品类别id集合
     * @return 响应的结果
     */
    @PostMapping("delete")
    public Result deleteGoodsCategory(@RequestParam("idList") List<Long> idList){
        if (CollectionUtils.isEmpty(idList)) return null;
        return handlerExecutor.execute(() -> categoryHandler.deleteGoodsCategory(idList));
    }

    /**
     * 根据店铺id查询商品分类
     * @param shopId 店铺id
     * @return 响应的结果
     */
    @PostMapping("findByShopId")
    public Result findGoodsCategoryByShopId(@RequestParam("shopId") Long shopId){
        return handlerExecutor.execute(() -> categoryHandler.findGoodsCategoryByShopId(shopId));
    }
}
